package com.example.neo4jdemo.service;

import com.example.neo4jdemo.dao.NodeDao;
import com.example.neo4jdemo.dao.NodeRepository;
import com.example.neo4jdemo.dao.TransferRepository;
import com.example.neo4jdemo.dao.UserRepository;
import com.example.neo4jdemo.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private TransferRepository transferRepository;
    @Autowired
    private NodeRepository nodeRepository;

    @Autowired
    private NodeDao nodeDao;


    @Transactional
    public void addUser(UserEntity userEntity){
        userRepository.save(userEntity);
    }


    public void saveAll(List<BitEntity> bitEntityList){
        bitEntityList.forEach(p->{
            NoEntity startEntity = new NoEntity();
            startEntity.setAge((int)Math.random()*50);
            startEntity.setSex(((int)Math.random()*10)/2==1?"男":"女");
            startEntity.setName(this.getName());
            startEntity.setIdCard(this.getIdCard());
            Node nodeFrom =  nodeDao.findByAddress(p.getFrom());
            if(null != nodeFrom){
                startEntity.setId(nodeFrom.getNeoId());
                startEntity.setAddress(nodeFrom.getAddress());

            }else{
                nodeFrom = new Node();
                startEntity.setAddress(p.getFrom());
                NoEntity noEntity = nodeRepository.save(startEntity);
                nodeFrom.setAddress(p.getFrom());
                nodeFrom.setCreateTime(new Date());
                nodeFrom.setNeoId(noEntity.getId());
                nodeFrom.setIsDelete(1);
                nodeFrom.setUpdateTime(new Date());
                nodeDao.save(nodeFrom);
            }

            NoEntity endEntity = new NoEntity();
            endEntity.setAge(new Random().nextInt(50));
            endEntity.setSex(new Random().nextInt(10)/2 ==1 ?"男":"女");
            endEntity.setName(this.getName());
            endEntity.setIdCard(this.getIdCard());
            Node nodeTo =  nodeDao.findByAddress(p.getTo());
            if(null != nodeTo){
                endEntity.setId(nodeTo.getNeoId());
                endEntity.setAddress(nodeTo.getAddress());
            }else{
                nodeTo = new Node();
                endEntity.setAddress(p.getTo());
                NoEntity noEntity = nodeRepository.save(endEntity);
                nodeTo.setAddress(p.getTo());
                nodeTo.setCreateTime(new Date());
                nodeTo.setNeoId(noEntity.getId());
                nodeTo.setIsDelete(1);
                nodeTo.setUpdateTime(new Date());
                nodeDao.save(nodeFrom);
            }

            TransferEntity transferEntity = new TransferEntity();
            transferEntity.setAmount(p.getAmount());
            transferEntity.setHash(p.getHash());
            transferEntity.setUnit(p.getUnit());
            transferEntity.setStartNode(startEntity);
            transferEntity.setEndNode(endEntity);
            transferRepository.save(transferEntity);

        });
    }

    public static String getName(){
        List<String> nameList = new ArrayList<>();
        nameList.add("高科成");
        nameList.add("高磊");
        nameList.add("黄成");
        nameList.add("李三要");
        nameList.add("李谷天");
        nameList.add("赵雨");
        nameList.add("青可");
        nameList.add("钟毓");
        nameList.add("大方");
        nameList.add("李忠");
        nameList.add("kirin");
        nameList.add("sine");
        nameList.add("siren");
        nameList.add("vic");
        nameList.add("gino");
        nameList.add("重中于");
        nameList.add("高科成");
        nameList.add("王岁");
        nameList.add("桂叁");
        nameList.add("李四广");
        nameList.add("李谷天");
        nameList.add("钟雨");
        nameList.add("赵可");
        nameList.add("钟毓");
        nameList.add("大方");
        nameList.add("李忠");
        nameList.add("fred");
        nameList.add("sine");
        nameList.add("simon");
        nameList.add("tom");
        nameList.add("coco");
        nameList.add("tim");
        int a = new Random().nextInt(30);
        return nameList.get(a);
    }
    public static String getIdCard(){
        Random r = new Random();
                StringBuilder rs = new StringBuilder();
                for (int i = 0; i < 18; i++) {
                       rs.append(r.nextInt(9)+1);
                   }
              return rs.toString();
    }

    public static void main(String[] args) {
        System.out.println(getName());
        System.out.println(getIdCard());
    }

}
